<?php

namespace Home\Controller;

class UploadController extends CommonController {

    public function member_avatar() {//个人头像 替换
        //判断是否在允许文件类型
        $path = C("avatarFile");
        $upload = new \Think\Upload(); // 实例化上传类
        $upload->maxSize = C('upload_max_size'); // 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'jpeg'); // 设置附件上传类型
        $upload->rootPath = $path; // 设置附件上传目录
      
        $upload->saveName = array('uniqid', '');
        $upload->autoSub = false;
        $info = $upload->upload();
        $uploadInfo = $info['file'];
        $savename = $uploadInfo['savename']; //数据库保存名
// $type = strtolower(substr(strrchr($savename, '.'), 1));
        if ($info) {
            $image = new \Think\Image();
            $image->open($path . $savename);
            $image->thumb(130, 130, \Think\Image::IMAGE_THUMB_FIXED)->save($path . "130_" . $savename);
            $rs = M("user")->where("id = " . session("userid") . "")->save(array("avatar" => $savename));
            
//            echo M("shop")->getlastsql();
        } else {
            $arr['error'] = $upload->getError();
            echo json_encode($arr);
            exit;
        }
        $arr = array(
            'name' => $savename,
            'pic' => __APP__ . "/" . $path . "130_" . $savename,
            'state' => $rs
        );
        echo json_encode($arr);
        exit;
    }

}

?>